#!/bin/bash
# LNMP For RHEL/CentOS
# Date: 2013-12-30
CWD=$(pwd)
. ./functions/soft_version.sh
. ./functions/mysql.sh
. ./functions/system.sh
. ./functions/logs.sh
. ./functions/nginx.sh
. ./functions/php4nginx.sh
. ./functions/php_modules.sh

system_check
welcome
check_current_user

echo -ne "Press any key to start install \033[32;49;1mLNMP\033[39;49;0m ... "
char=`get_char`
echo
echo

# 时间校对
#NTP_CN

#系统优化
#Optimization

############# Check Packages ################
logs "Info" "Checking Packages"

rpm -q gcc gcc-c++ autoconf make cmake libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glibc-common glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel libtool libtool-libs libevent-devel libevent libtool-ltdl libtool-ltdl-devel bison bison-devel gettext zip gd gd-devel pcre pcre-devel expect | grep "not installed" >> ${logs_file}

############# Install Packages ##############
logs "Info" "Install System Packages"

yum -y remove httpd nginx php mysql-server mysql mysql-libs php-mysql

yum -y install gcc gcc-c++ autoconf make cmake libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glibc-common glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel libtool libtool-libs libevent-devel libevent libtool-ltdl libtool-ltdl-devel bison bison-devel gettext zip gd gd-devel pcre pcre-devel expect crontabs sendmail patch

############### Install MySQL #################

logs "Info" "Install MySQL"

MySQL_Install

############ Ready for install PHP  ##############
logs "Info" "Install libiconv"

cd $CWD
tar -zxf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local
make && make install
cd ..

logs "Info" "Install libmcrypt"
tar -zxf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
make && make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make && make install
cd ..

logs "Info" "Install mhash"

cd $CWD
tar -zxf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9
./configure
make && make install
cd ..

logs "Info" "Create soft link for libmcrypt"

if [ "$bit" = "x64" ]; then
ln -s /usr/local/lib/libmcrypt.la /usr/lib64/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib64/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib64/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib64/libmcrypt.so.4.4.8
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
ln -s /usr/local/lib/libiconv.so.2 /usr/lib64/libiconv.so.2
ln -s /usr/local/lib/libmhash.a /usr/lib64/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib64/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib64/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib64/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib64/libmhash.so.2.0.1
else
ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libiconv.so.2 /usr/lib/libiconv.so.2
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
fi

logs "Info" "Install mcrypt"

tar -zxf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
./configure
make && make install
cd ..

logs "Info" "Create sotf links for libs(libsmysqlclient,libjpeg,libpng)"
if [ "$bit" = "x64" ]; then
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18
ln -s /usr/lib64/libjpeg.so /usr/lib/libjpeg.so 
ln -s /usr/lib64/libldap.so /usr/lib/libldap.so 
ln -s /usr/lib64/libpng.so /usr/lib/libpng.so
else
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18
fi

/sbin/ldconfig

############ Install PHP #############

[ "$phpv" = "5.3" ] && php53
[ "$phpv" = "5.2" ] && php52

########## PHP Modules Install ########
php_modules_install

############ Starting PHP #############
if [ "$phpv" = "5.3" ] ; then
   /etc/init.d/php-fpm start
else
   /usr/local/php/sbin/php-fpm start
fi

###############  Install Nginx ################
logs "Info" "Install Nginx"

cd $CWD
Nginx_Install
cd ..

logs "Info" "Install phpMyAdmin"

cd $CWD
tar -jxf phpMyAdmin-$pmVer.tar.bz2
mv phpMyAdmin-$pmVer /var/www/localhost/phpmyadmin
chown www.www -R /var/www/localhost

logs "Info" "Install complete"

# get localhost IP address
ip=`ifconfig | grep "inet addr" | grep -v "127.0.0.1" | awk -F: '{print $2}' | awk '{print $1}' | head -n 1`
echo
echo "Congratulations!! "
echo "Linux + Nginx + PHP is Sucessfully! "
echo
echo "========================== Installation information ====================="
echo "                 Install LNMP 2013 Final completed! Enjoy it."
echo "========================================================================="
echo "        LNMP V2013 for RadHat/CentOS Linux Written by Luxiaok.com "
echo "========================================================================="
echo ""
echo "     For more information please visit http://cto.luxiaok.com/"
echo ""
echo "index page: http://$ip/index.php"
echo "   phpinfo: http://$ip/info.php"
echo "phpMyAdmin: http://$ip/phpmyadmin"
echo "   iProber: http://$ip/tz.php"
echo ""
#echo "default MySQL root password:$mysqlrootpwd"
echo ""
echo "The path of LNMP directorys:"
echo " MySQL DIR:   /usr/local/mysql"
echo "   PHP DIR:   /usr/local/php"
echo " Nginx DIR:   /usr/local/nginx"
echo "   WEB DIR:   /var/www"
echo " Vhost DIR:   /var/www/*/.vhost"
echo ""
echo "=========================================================================="

cat >> $CWD/tips.txt << EOF
Some information for LNMP:
     Index: http://$ip/index.php
   phpinfo: http://$ip/info.php
phpMyAdmin: http://$ip/phpmyadmin
   iProber: http://$ip/tz.php
    
Default MySQL root password is null

The path of LNMP directorys:
MySQL DIR: /usr/local/mysql
Conf file: /usr/lcao/mysql/etc/my.cnf
PHP  DIR : /usr/local/php
Conf file: /usr/local/php/etc/php.ini
Nginx DIR: /usr/local/nginx
Conf file: /usr/local/nginx/conf/nginx.conf
  WEB DIR: /var/www/
Vhost DIR: /var/www/*/.vhost
EOF
netstat -ntlup